#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>

#include "idc_ubus.h"
#include "idc_ubus_mesh.h"

static const struct ubus_method whqx_mesh_methods[] = {
        UBUS_METHOD("info", ubus_whqx_mesh_info, default_policy),
        UBUS_METHOD("reboot", ubus_whqx_mesh_reboot, default_policy),
        UBUS_METHOD("reset", ubus_whqx_mesh_reset, default_policy),
};
static const struct ubus_method yunw_mesh_methods[] = {
        UBUS_METHOD("status", ubus_yunw_mesh_status, default_policy),
}; 
static struct ubus_object_type whqx_mesh_object_type =  UBUS_OBJECT_TYPE("sihan.mesh.whqx", whqx_mesh_methods);
static struct ubus_object_type yunw_mesh_object_type =  UBUS_OBJECT_TYPE("sihan.mesh.yunw", yunw_mesh_methods); 
 
static struct ubus_object whqx_mesh_object = {
        .name = "sihan.mesh.whqx",
        .type = &whqx_mesh_object_type,
        .methods = whqx_mesh_methods,
        .n_methods = ARRAY_SIZE(whqx_mesh_methods)
};

static struct ubus_object yunw_mesh_object = {
        .name = "sihan.mesh.yunw",
        .type = &yunw_mesh_object_type,
        .methods = yunw_mesh_methods,
        .n_methods = ARRAY_SIZE(yunw_mesh_methods)
};
 
int ubus_doing()
{
        int ret;
 
        ctx = ubus_connect(NULL);
        if (!ctx) {
                fprintf(stderr, "Failed to connect to ubus\n");
                return -1;
        }
        ubus_add_uloop(ctx);
 
        ret = ubus_add_object(ctx, &whqx_mesh_object);
        if (ret)
                fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
        ret = ubus_add_object(ctx, &yunw_mesh_object);
        if (ret)
                fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
        #if 0
        ret = ubus_register_subscriber(ctx, &test_event);
	if (ret)
		fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
        #endif
}
void *ubus_worker(void *arg)
{
        printf("idc_ubus_init\n");
        uloop_init();
        ubus_doing();
        uloop_run();
        ubus_free(ctx);
        uloop_done();
}
